package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.CharacterStyle;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.ShopActivity;
import cn.xfdzx.android.apps.shop.activity.XFDShopDetailsActivity;
import cn.xfdzx.android.apps.shop.bean.SearchBeen;
import cn.xfdzx.android.apps.shop.bean.SearchItemBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.view.TagTextView;

/**
 * @author gzk
 * @date 2020/3/27 0027
 * description：搜索的适配器
 */
public class SearchItemAdapter extends RecyclerView.Adapter {
    private ItemClickListener mItemClickListener;
    /**
     * 类型1：分类
     * banner实现
     */
    public static final int TYPE_TITLE = 0;
    /**
     * 类型2：商家
     */
    public static final int TYPE_CONTENT = 1;

    private SearchBeen searchBeen;
    /**
     * 当前类型
     */
    public int mCurrentType = TYPE_TITLE;

    private Context mContext;
    private final LayoutInflater mLayoutInflater;
    private RecyclerView rvSearchItemType;
    private RelativeLayout rlSearchType;
    private int type;
    private String keyWrod = "";

    public SearchItemAdapter(Context mContext, RecyclerView rvSearchItemType, RelativeLayout rlSearchType, int type) {
        this.mContext = mContext;
        this.rvSearchItemType = rvSearchItemType;
        this.rlSearchType = rlSearchType;
        this.type = type;

        //以后用它来初始化布局
        mLayoutInflater = LayoutInflater.from(mContext);

//        addData();
    }

    public void setData(SearchBeen searchBeen,String keyWrod) {
        this.searchBeen = searchBeen;
        this.keyWrod = keyWrod;
        notifyDataSetChanged();
    }

    private void addData() {
//        mList.add(new SearchItemBeen("苹果", 1, "", "", "", ""));
//        mList.add(new SearchItemBeen("橘子", 1, "", "", "", ""));
//        mList.add(new SearchItemBeen("香蕉", 1, "", "", "", ""));
//        mList.add(new SearchItemBeen("蔬菜", 1, "", "", "", ""));
//        mList.add(new SearchItemBeen("", R.drawable.image_avatar_1, "新发地掌鲜地", "起送费¥5", "基础运费¥5", "24.0km"));
//        mList.add(new SearchItemBeen("", R.drawable.image_avatar_2, "新发地掌鲜地", "起送费¥5", "基础运费¥5", "24.0km"));
//        mList.add(new SearchItemBeen("", R.drawable.image_avatar_3, "新发地掌鲜地", "起送费¥5", "基础运费¥5", "24.0km"));
//        mList.add(new SearchItemBeen("", R.drawable.image_avatar_4, "新发地掌鲜地", "起送费¥5", "基础运费¥5", "24.0km"));
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_TITLE) {
            return new SearchTypeViewHolder(mContext, mLayoutInflater.inflate(R.layout.item_search_type_textview, null));
        } else if (viewType == TYPE_CONTENT) {
            if (type == 0) {
                return new SearchContentViewHolder(mContext, mLayoutInflater.inflate(R.layout.item_search_shop, null));
            } else {
                return new SearchXFDContentViewHolder(mContext, mLayoutInflater.inflate(R.layout.item_search_xfd_shop, null));
            }
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case TYPE_TITLE:
                ((SearchTypeViewHolder) holder).setData(searchBeen.getData().getGoods().get(position), position);
                break;

            case TYPE_CONTENT:
                if (type == 0) {
                    ((SearchContentViewHolder) holder).setData(searchBeen.getData().getStore().get(position - searchBeen.getData().getGoods().size()));
                } else {
                    ((SearchXFDContentViewHolder) holder).setData(searchBeen.getData().getStore().get(position - searchBeen.getData().getGoods().size()));
                }

                break;

        }
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {

        return searchBeen == null ? 0 : searchBeen.getData().getGoods().size() + searchBeen.getData().getStore().size();
    }

    @Override
    public int getItemViewType(int position) {
        if (position < searchBeen.getData().getGoods().size()) {
            mCurrentType = TYPE_TITLE;
        } else {
            mCurrentType = TYPE_CONTENT;
        }
        return mCurrentType;
    }

    public class SearchTypeViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.tv_search_type_text)
        TextView tvSearchTypeText;
        @BindView(R.id.rl_search_type)
        RelativeLayout rlItemType;
        private Context mContext;


        public SearchTypeViewHolder(Context mContext, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mContext = mContext;

        }


        public void setData(SearchBeen.DataBean.GoodBean categoryArrayBean, int position) {
            int startindex = categoryArrayBean.getTitle().indexOf(keyWrod);
            //tvSearchTypeText.setText(categoryArrayBean.getTitle());
            SpannableStringBuilder style = new SpannableStringBuilder(categoryArrayBean.getTitle());
            style.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.color_eb5c20)), startindex,startindex + keyWrod.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
              //style.setSpan(new BackgroundColorSpan(mContext.getResources().getColor(R.color.color_FE4e00)), 0, 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            tvSearchTypeText.setText(style);

            rlItemType.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isFastClick()) {
                        rvSearchItemType.setVisibility(View.GONE);
                        rlSearchType.setVisibility(View.VISIBLE);
                        mItemClickListener.onItemClick(position);
                    }
                }
            });

        }
    }

    public class SearchContentViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.rl_item_search_shop)
        RelativeLayout rlItemSearchShop;
        @BindView(R.id.iv_search_shop)
        ImageView ivSearchShop;
        @BindView(R.id.tv_search_shop_name)
        TextView tvSearchShopName;//店铺名称
        @BindView(R.id.tv_search_start_money)
        TextView tvSearchStartMoney;//起送费
        @BindView(R.id.tv_search_basis_money)
        TextView tv_search_basis_money;//基础费
        @BindView(R.id.tv_search_distance_money)
        TextView tv_search_distance_money;//距离
        @BindView(R.id.tv_dayang)
        TextView tvDayang;//打烊


        private Context mContext;

        public SearchContentViewHolder(Context mContext, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mContext = mContext;
        }


        public void setData(SearchBeen.DataBean.StoreBean storeBean) {
            RoundedCorners roundedCorners = new RoundedCorners(20);
            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
            options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                    .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                    .error(R.drawable.icon_defult);
//            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
            Glide.with(mContext).load(storeBean.getLogo()).apply(options.transform(new CenterCrop(), roundedCorners)).into(ivSearchShop);

            if (type == 0) {
                tvSearchShopName.setText(storeBean.getOffline_shop_name());
                if (storeBean.getIs_close() == 0) {
                    tvDayang.setVisibility(View.GONE);
                } else {
                    tvDayang.setVisibility(View.VISIBLE);
                }
            } else {
                tvSearchShopName.setText(storeBean.getName());
            }
            tvSearchStartMoney.setText("起送¥" + storeBean.getSend_fees());
            tv_search_basis_money.setText("基础运费¥" + storeBean.getBase_freight());
            if (storeBean.getDistance() == null) {
                tv_search_distance_money.setText("");
            } else {
                tv_search_distance_money.setText(storeBean.getDistance() + "");
            }
//            tv_search_distance_money.setText(storeBean.getDis()+"km");
            rlItemSearchShop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isFastClick()) {
                        Intent intent = new Intent(mContext, ShopActivity.class);
                        intent.putExtra("shop_id", storeBean.getId() + "");
                        mContext.startActivity(intent);
                    }
                }
            });

        }
    }

    public class SearchXFDContentViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.rl_xfd_shop)
        RelativeLayout rlXfdShop;
        @BindView(R.id.ttv_shop_name)
        TagTextView ttvShopName;
        @BindView(R.id.tv_the_main)
        TextView tvTheMain;//
        @BindView(R.id.tv_qu)
        TextView tvQu;//

        private Context mContext;

        public SearchXFDContentViewHolder(Context mContext, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mContext = mContext;
        }


        public void setData(SearchBeen.DataBean.StoreBean storeBean) {
            ttvShopName.setSingleTagAndContent("新发地", storeBean.getName(), R.drawable.d02c557_bg_r3, "#FFFFFFFF");
            tvTheMain.setText(storeBean.getSalecate());
            tvQu.setText(storeBean.getStore_current_address());

            rlXfdShop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isFastClick()) {
                        Intent intent = new Intent(mContext, XFDShopDetailsActivity.class);
                        intent.putExtra("shop_id", storeBean.getId() + "");
                        mContext.startActivity(intent);
                    }
                }
            });

        }
    }

    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }
}
